package com.fit.utils.se;

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.Flushable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * IO工具
 * @author hcl
 *
 */
public class IOUtil {
	private static Logger log = LoggerFactory.getLogger(IOUtil.class);
	
	//默认的编码
	private static final String DEFAULT_ENCODING = "UTF-8";
	
	/**
	 * 资源关闭方法(按顺序关闭)
	 * @param closeables
	 */
	public static void close(Closeable... closeables){
		if(EmptyUtils.isNotEmpty(closeables)){
			for(Closeable closeable : closeables){
				if(EmptyUtils.isNotEmpty(closeable)){
					 try {
                    	try{
	                    	if(closeable instanceof Flushable){
	                    		((Flushable) closeable).flush();
	                    	}
                    	}finally{
                    		closeable.close();
                    	}
					} catch (IOException e) {
						log.error("关闭流异常",e);
					}
				}
			}
		}
	}
	
	/**
	 * 其他资源关闭方法(按顺序关闭)
	 * @param closeObj
	 */
	public static void closeResources(Object... closeObj){
		if(EmptyUtils.isNotEmpty(closeObj)){
			for(Object closeable : closeObj){
				if(EmptyUtils.isNotEmpty(closeable)){
					 try {
                    	try{
	                    	if(closeable instanceof Flushable){
	                    		((Flushable) closeable).flush();
	                    	}
                    	}finally{
                    		if(closeable instanceof Closeable){
                    			((Closeable)closeable).close();
                    		}else{
                    			Method m = ReflectUtil.getMethod(closeable.getClass(), "close");
                    			if(EmptyUtils.isNotEmpty(m)){
                    				ReflectUtil.executeMethod(closeable, m);
                    			}
                    		}
                    	}
					} catch (IOException e) {
						log.error("关闭流异常",e);
					}
				}
			}
		}
	}
	
	
	
	
	/**
	 * 将输入流to 为byte[]
	 * @param in 输入流
	 * @return
	 * @throws IOException 
	 */
	public static byte[] toByte(InputStream in) throws IOException {
		ByteArrayOutputStream out = null;
		byte[] resultByte = null;
		try{
			if(EmptyUtils.isNotEmpty(in)){
				out = new ByteArrayOutputStream();
				byte[] b = new byte[1024];
				int len = -1;
				while((len = in.read(b)) != -1){
					out.write(b, 0, len);
				}
				resultByte = out.toByteArray();
			}
		}finally{
			IOUtil.close(in,out);
		}
		return resultByte;
	}
	
	/**
	 * 将输入流to为字符串
	 * @param in 输入流
	 * @param encoding 字符编码
	 * @return
	 * @throws IOException
	 */
	public static String toString(InputStream in,String encoding) throws IOException{
		return new String(IOUtil.toByte(in), encoding);
	}
	
	/**
	 * 将输入流to为字符串
	 * @param in 输入流
	 * @return
	 * @throws IOException
	 */
	public static String toString(InputStream in) throws IOException{
		return new String(IOUtil.toByte(in), DEFAULT_ENCODING);
	}
	
	
	
	/**
	 * 将输入流中的内容传copy到输出流中
	 * @param in 输入流
	 * @param out 输出流
	 * @throws IOException 
	 */
	public static void copy(InputStream in,OutputStream out) throws IOException{
		try{
			if(EmptyUtils.isNotEmpty(in) && EmptyUtils.isNotEmpty(out)){
				byte[] b = new byte[1024];
				int len = -1;
				while((len = in.read(b)) != -1){
					out.write(b, 0, len);
				}
			}
		}finally{
			IOUtil.close(in,out);
		}
	}
	
	/**
	 * 将第一个文件复制到第二个文件中(必须保证文件存在,此方法不会自动创建文件)
	 * @param inFile 被copy的文件
	 * @param outFile 目标文件
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static void copy(File inFile, File outFile) throws FileNotFoundException, IOException{
		if(EmptyUtils.isNotEmpty(inFile) && EmptyUtils.isNotEmpty(outFile)){
			IOUtil.copy(new FileInputStream(inFile), new FileOutputStream(outFile));
		}
	}
}
